// Regional variants configuration
export const regionalVariants: { [key: string]: { [key: string]: string } } = {
  // Standard Mandarin (default)
  "mandarin": {
    // Direct relationships
    "爸爸": "父亲",
    "妈妈": "母亲",
    "儿子": "儿子",
    "女儿": "女儿",
    "哥哥": "哥哥",
    "弟弟": "弟弟",
    "姐姐": "姐姐",
    "妹妹": "妹妹",
    
    // Paternal side
    "爸爸的爸爸": "爷爷",
    "爸爸的妈妈": "奶奶",
    "爸爸的哥哥": "伯伯",
    "爸爸的弟弟": "叔叔",
    "爸爸的姐姐": "姑妈",
    "爸爸的妹妹": "姑姑",
    
    // Maternal side
    "妈妈的爸爸": "外公",
    "妈妈的妈妈": "外婆",
    "妈妈的哥哥": "舅舅",
    "妈妈的弟弟": "舅舅",
    "妈妈的姐姐": "姨妈",
    "妈妈的妹妹": "姨妈",
    
    // Children's children
    "儿子的儿子": "孙子",
    "儿子的女儿": "孙女",
    "女儿的儿子": "外孙",
    "女儿的女儿": "外孙女",
    
    // Siblings' children
    "哥哥的儿子": "侄子",
    "哥哥的女儿": "侄女",
    "弟弟的儿子": "侄子",
    "弟弟的女儿": "侄女",
    "姐姐的儿子": "外甥",
    "姐姐的女儿": "外甥女",
    "妹妹的儿子": "外甥",
    "妹妹的女儿": "外甥女",

    // Marriage relationships
    "丈夫": "丈夫",
    "妻子": "妻子",
    "丈夫的爸爸": "公公",
    "丈夫的妈妈": "婆婆",
    "妻子的爸爸": "岳父",
    "妻子的妈妈": "岳母",
    "丈夫的哥哥": "大伯",
    "丈夫的弟弟": "小叔",
    "丈夫的姐姐": "大姑",
    "丈夫的妹妹": "小姑",
    "妻子的哥哥": "大舅",
    "妻子的弟弟": "小舅",
    "妻子的姐姐": "大姨",
    "妻子的妹妹": "小姨",
    
    // Adoption relationships
    "养父": "养父",
    "养母": "养母",
    "养子": "养子",
    "养女": "养女",
    "继父": "继父",
    "继母": "继母",
    "继子": "继子",
    "继女": "继女",
    
    // Extended marriage relationships
    "儿媳": "儿媳",
    "女婿": "女婿",
    "妯娌": "妯娌",
    "连襟": "连襟",
  },
  
  // Cantonese (粤语)
  "cantonese": {
    "爸爸": "老豆",
    "妈妈": "老母",
    "爸爸的爸爸": "阿爷",
    "爸爸的妈妈": "阿嫲",
    "爸爸的哥哥": "大伯",
    "爸爸的弟弟": "阿叔",
    "妈妈的爸爸": "外公",
    "妈妈的妈妈": "外婆",
    "妈妈的哥哥": "舅父",
    "妈妈的弟弟": "舅父",
    "妈妈的姐姐": "姨妈",
    "妈妈的妹妹": "姨妈",
    "哥哥": "大佬",
    "弟弟": "细佬",
    "姐姐": "家姐",
    "妹妹": "细妹",
  },
  
  // Shanghainese (上海话)
  "shanghainese": {
    "爸爸": "爹爹",
    "妈妈": "妈妈",
    "爸爸的爸爸": "阿爷",
    "爸爸的妈妈": "阿娘",
    "爸爸的哥哥": "伯伯",
    "爸爸的弟弟": "叔叔",
    "妈妈的爸爸": "外公",
    "妈妈的妈妈": "外婆",
    "妈妈的哥哥": "娘舅",
    "妈妈的弟弟": "娘舅",
  },
  
  // Sichuan dialect (四川话)
  "sichuan": {
    "爸爸": "老汉",
    "妈妈": "妈",
    "爸爸的爸爸": "爷爷",
    "爸爸的妈妈": "奶奶",
    "爸爸的哥哥": "伯伯",
    "爸爸的弟弟": "叔叔",
    "妈妈的爸爸": "外公",
    "妈妈的妈妈": "外婆",
    "妈妈的哥哥": "舅子",
    "妈妈的弟弟": "舅子",
    "哥哥": "哥老倌",
    "弟弟": "兄弟",
  },
};

// Default relationship map for backward compatibility
export const relationshipMap: { [key: string]: string } = regionalVariants.mandarin;

// Function to calculate relationship with regional support
export function calculateRelationship(input: string, region: string = 'mandarin'): string {
  // Normalize input: remove spaces, handle "我" prefix
  let normalizedInput = input.trim().replace(/\s+/g, '');
  if (normalizedInput.startsWith('我')) {
    normalizedInput = normalizedInput.substring(1);
  }
  
  // Check if the relationship is in the regional map
  const regionMap = regionalVariants[region] || regionalVariants.mandarin;
  if (regionMap[normalizedInput]) {
    return regionMap[normalizedInput];
  }
  
  // Fallback to mandarin if not found in regional map
  if (region !== 'mandarin' && relationshipMap[normalizedInput]) {
    return relationshipMap[normalizedInput];
  }
  
  // If not found, return a default message
  return "未知关系";
}

// Enhanced reverse lookup function with regional support
export function reverseLookup(call: string, region: string = 'mandarin'): string {
  const results: string[] = [];
  
  // Find all relationship paths for a given call in regional map
  const regionMap = regionalVariants[region] || regionalVariants.mandarin;
  for (const [key, value] of Object.entries(regionMap)) {
    if (value === call) {
      results.push(`我${key}`);
    }
  }
  
  // Fallback to mandarin if no results found
  if (results.length === 0) {
    for (const [key, value] of Object.entries(relationshipMap)) {
      if (value === call) {
        results.push(`我${key}`);
      }
    }
  }
  
  if (results.length > 0) {
    return results.join(' 或 ');
  }
  
  // Handle some common variations
  const variations: { [key: string]: string } = {
    "伯父": "爸爸的哥哥",
    "叔父": "爸爸的弟弟",
    "舅父": "妈妈的哥哥或妈妈的弟弟",
    "姨母": "妈妈的姐姐或妈妈的妹妹",
    "祖父": "爸爸的爸爸",
    "祖母": "爸爸的妈妈",
    "外祖父": "妈妈的爸爸",
    "外祖母": "妈妈的妈妈",
  };
  
  if (variations[call]) {
    return variations[call];
  }
  
  return "未知称呼";
}

// Function to handle multi-level relationship parsing with regional support
export function parseComplexRelationship(relationship: string, region: string = 'mandarin'): string {
  // Remove "我" prefix if present
  let normalized = relationship.replace(/^我/, '');
  
  // Split by "的" to get relationship steps
  const steps = normalized.split('的');
  
  if (steps.length === 1) {
    // Single step relationship
    return calculateRelationship(steps[0], region);
  }
  
  // Handle multi-step relationships
  let currentRelationship = steps[0];
  let result = '';
  
  for (let i = 1; i < steps.length; i++) {
    const combined = currentRelationship + '的' + steps[i];
    
    // Check if this combined relationship exists in our map
    const regionMap = regionalVariants[region] || regionalVariants.mandarin;
    if (regionMap[combined]) {
      result = regionMap[combined];
      currentRelationship = result;
    } else {
      // If not found, try to build step by step
      const tempResult = calculateRelationship(combined, region);
      if (tempResult !== "未知关系") {
        result = tempResult;
        currentRelationship = result;
      } else {
        // For complex chains, use a recursive approach
        const previousCall = calculateRelationship(currentRelationship, region);
        if (previousCall !== "未知关系") {
          // Build new relationship path
          const newPath = previousCall + '的' + steps[i];
          const newResult = calculateRelationship(newPath, region);
          if (newResult !== "未知关系") {
            result = newResult;
            currentRelationship = result;
          } else {
            return "无法解析的复杂关系";
          }
        } else {
          return "无法解析的复杂关系";
        }
      }
    }
  }
  
  return result || "无法解析的复杂关系";
}

// Extended relationship map for complex relationships
export const extendedRelationshipMap: { [key: string]: string } = {
  ...regionalVariants.mandarin,
  // Marriage relationships
  "丈夫": "丈夫",
  "妻子": "妻子",
  "丈夫的爸爸": "公公",
  "丈夫的妈妈": "婆婆",
  "妻子的爸爸": "岳父",
  "妻子的妈妈": "岳母",
  "丈夫的哥哥": "大伯",
  "丈夫的弟弟": "小叔",
  "丈夫的姐姐": "大姑",
  "丈夫的妹妹": "小姑",
  "妻子的哥哥": "大舅",
  "妻子的弟弟": "小舅",
  "妻子的姐姐": "大姨",
  "妻子的妹妹": "小姨",
  
  // Adoption relationships
  "养父": "养父",
  "养母": "养母",
  "养子": "养子",
  "养女": "养女",
  "继父": "继父",
  "继母": "继母",
  "继子": "继子",
  "继女": "继女",
  
  // Extended marriage relationships
  "儿媳": "儿媳",
  "女婿": "女婿",
  "妯娌": "妯娌",
  "连襟": "连襟",
  
  // Complex relationships
  "哥哥的爸爸": "爸爸",
  "哥哥的妈妈": "妈妈",
  "姐姐的爸爸": "爸爸",
  "姐姐的妈妈": "妈妈",
  "弟弟的爸爸": "爸爸",
  "弟弟的妈妈": "妈妈",
  "妹妹的爸爸": "爸爸",
  "妹妹的妈妈": "妈妈",
  
  // Cross-generation relationships
  "爷爷的爸爸": "曾祖父",
  "爷爷的妈妈": "曾祖母",
  "奶奶的爸爸": "外曾祖父",
  "奶奶的妈妈": "外曾祖母",
  "外公的爸爸": "外曾祖父",
  "外公的妈妈": "外曾祖母",
  "外婆的爸爸": "外曾祖父",
  "外婆的妈妈": "外曾祖母",
  
  // Siblings' grandchildren
  "哥哥的孙子": "侄孙",
  "哥哥的孙女": "侄孙女",
  "弟弟的孙子": "侄孙",
  "弟弟的孙女": "侄孙女",
  "姐姐的孙子": "外甥孙",
  "姐姐的孙女": "外甥孙女",
  "妹妹的孙子": "外甥孙",
  "妹妹的孙女": "外甥孙女",
};